using System.Collections.Generic;
using UnityEngine;
using MyTools;
//所有玩家、敌人、环境物品生物都具有的组件
//必须重写的方法：
//Dead() 死亡
namespace BaseScripts{
    
    # region "Effect"

    public enum EffectType{
        None,
        NormalDamage,  //在Effect构造函数value属性中填入:(value[0])伤害值
        AddForce, //在Effect构造函数value属性中填入:(value[0], value[1], value[2])作为作用力的方向, value[3]为作用力的大小(若无则大小为1)
        GetHit, //通知被击中, 调用Status.GetHit函数.
    }   
    /// <summary>
    /// 构造对Status影响的主要方式,
    /// 用法: 
    /// obj.GetComponent<Status>().AddEffect(
    ///     new Status.Effect(obj.GetComponent<Status>(), 
    ///     Status.Effect.Type.NormalDamage, 
    ///     10)
    /// ) 
    ///  </summary>
    public class Effect{
        public Effect(Status status, EffectType type, float[] value){
            this.status = status;
            this.type = type;

            this.Values = new List<float>(capacity: value.Length);
            for (int i = 0; i < value.Length; i++)
            {
                this.Values.Add(value[i]);
            }
        }

        public Status status;
        public EffectType type = EffectType.None;
        public List<float> Values ;

        public bool bIsWorked = false;
        public bool bIsOver = false;
        public void Update(){
            if (bIsOver) return;
            switch (type)
            {
                case EffectType.NormalDamage:
                    if (!bIsWorked){
                        bIsWorked = true;
                        bIsOver = true;
                        status.ChangeLife(-Values[0]);
                    }
                    break;
                case EffectType.AddForce:
                    if (!bIsWorked){
                        bIsWorked = true;
                        bIsOver = false;
                        float fForce = 1f;
                        if (Values.Count >= 4){
                            fForce = Values[3];
                        }
                        status.bCanMove = false;
                        status.cd_CanotMove.CooldownImidiately();
                        status.cd_CanotMove.Used();
                        status.rb.AddForce(new Vector2(Values[0], Values[1]).normalized*fForce*100f);
                    }else{
                        if (status.cd_CanotMove.Check()){
                            status.bCanMove = true;
                            bIsOver = true;
                        }
                    }
                    break;
                case EffectType.GetHit:
                    if (!bIsWorked){
                        status.GetHit();
                        bIsWorked = true;
                        bIsOver = true;
                    }
                    break;
                
                
                default: break;
            }
        }
    } 



    # endregion


public class Status : MonoBehaviour{

    # region "主要属性"
    public float maxLife = 100;
    
    protected float nowLife;

    public float defense = 0;

    public float atkRate = 1;// * 1

    public float speed = 1;// 基础速度
    public float speedRate = 1;// 速度增加倍率
    public float accelSpeed;// 加速度

    public float atkSpeedRate = 1;// * 1
    # endregion

    # region "状态暴露"
    public bool bCanMove = true;
    public float fCanotMoveTime = 0.5f;
    public Cooldown cd_CanotMove;

    public bool bEffectAble = true;
    public float fEffectAbleTime = 0.5f;
    public Cooldown cd_EffectAble;

    # endregion 


    public Rigidbody2D rb;
    protected GameObject GO_uiHealthBarPool;
    protected HealthBarPool healthBarPool;
    protected bool isAlive;
    protected List<Effect> effects;
    private List<Effect> effectsToRemove;

    protected virtual void Awake() {
        effects = new List<Effect>();
        effectsToRemove = new List<Effect>();
    }

    protected virtual void Start() {
        rb = GetComponent<Rigidbody2D>();
        nowLife = maxLife;
        FindHealthBarPool();

        cd_CanotMove = new Cooldown(fCanotMoveTime);
        cd_EffectAble = new Cooldown(fEffectAbleTime, ()=>{
            bEffectAble = true;
        });
    }

    protected virtual void Update() {
        cd_CanotMove.Update(Time.deltaTime);
    }


    protected virtual void FixedUpdate(){
        foreach (var each in effects){
            each.Update();
            if(each.bIsOver) effectsToRemove.Add(each);
        }
        effects.RemoveAll(each => effectsToRemove.Contains(each));
        effectsToRemove.Clear();
    }

    
    public void AddEffect(Effect effect){
        effects.Add(effect);
    }

    # region"HP 相关"

    public virtual void GetHit(){
        cd_EffectAble.StartCoolDown();
        bEffectAble = false;
    }

    public void ChangeLife(float delta){
        nowLife += delta;
        healthBarPool.ShowHealthBar(transform,nowLife/maxLife);
        CheckDead();
    }

    public bool CheckDead(){
        if(nowLife <= 0){
            isAlive = false;
            Dead();
            return true;
        }
        return false;
    } 

    protected virtual void Dead(){}


    private void FindHealthBarPool(){
        GO_uiHealthBarPool = GameObject.Find("UIHealthBarPool");
        // 检查是否成功找到
        if (GO_uiHealthBarPool == null)
        {
            Debug.LogError("未找到名为 UIHealthBarPool 的 GameObject！");
        }else{
            healthBarPool = GO_uiHealthBarPool.GetComponent<HealthBarPool>();
        }
    }
    # endregion
}


}